package com.sljy.hzk.view.adapter;

import android.app.Dialog;
import android.content.Context;
import android.database.DataSetObserver;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sljy.hzk.R;
import com.sljy.hzk.animation.AnimationCallType;
import com.sljy.hzk.animation.AnimationManager;
import com.sljy.hzk.base.UserInfo;
import com.sljy.hzk.entity.TaskBodyEntity;
import com.sljy.hzk.entity.TaskDatabaseEntity;
import com.sljy.hzk.entity.TaskDetailEntity;
import com.sljy.hzk.entity.TaskEntity;
import com.sljy.hzk.room.AppDataBase;
import com.sljy.hzk.room.dao.TaskBodyDao;
import com.sljy.hzk.room.dao.TaskDao;
import com.sljy.hzk.room.dao.UserDao;
import com.sljy.hzk.ui.task.TaskMainActivity;
import com.sljy.hzk.util.EventListenerManager;
import com.sljy.hzk.util.NetWorkUtils;
import com.sljy.hzk.util.TimeUtils;
import com.sljy.hzk.view.FocusEffct;
import com.tkk.api.RxEventProcessor;

import java.util.List;

public class TaskItemAdapter  extends BaseAdapter {

    private List<TaskEntity> Datas;
    private Context mContext;
    UserDao userDao;
    TaskDao taskDao;

    public TaskItemAdapter(List<TaskEntity> datas, Context mContext) {
        Datas = datas;
        userDao = AppDataBase.getInstance().getUserDao();
        taskDao = AppDataBase.getInstance().getTaskDao();
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return Datas.size();
    }

    @Override
    public Object getItem(int position) {
        return Datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder=new ViewHolder();
            //可以理解为从vlist获取view 之后把view返回给ListView
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_task,parent,false);
            holder.posNameStr = (TextView)convertView.findViewById(R.id.posName);
            holder.contentStr = (TextView) convertView.findViewById(R.id.title_content);
            holder.timeStr = (TextView) convertView.findViewById(R.id.timeText);
            holder.tips = (TextView) convertView.findViewById(R.id.tips);
            holder.personBtn = (Button)convertView.findViewById(R.id.personBtn);
            holder.doBtn = (LinearLayout) convertView.findViewById(R.id.doBtn);
            holder.stateBg = (ImageView) convertView.findViewById(R.id.stateBg);
            //holder.focus = (FocusEffct) convertView.findViewById(R.id.focusEffct);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }
        TaskEntity d = Datas.get(position);
        String[] p = d.getPartition().split("-");
        holder.posNameStr.setText(p[p.length-1]);
        if(d.isDanger())
            holder.contentStr.setText(Html.fromHtml("<font color='red' size = '24'>（隐患任务）</font>" + d.getTitle()) );
        else
            holder.contentStr.setText(d.getTitle());
        holder.timeStr.setText(TimeUtils.dateState(d.getHours()));
        Gson json = new Gson();
        //根据剩余时间调整显示状态
        Integer s = TimeUtils.dateStateString(d.getHours());
        switch (s){
            case 0:
                holder.stateBg.setBackgroundResource(R.drawable.timeblue);
                break;
            case 1:
                holder.stateBg.setBackgroundResource(R.drawable.timeorange);
                break;
            case 2:
                holder.stateBg.setBackgroundResource(R.drawable.timepurple);
                break;
        }
        //改变按钮颜色
        switch (d.getFlag()){
            case 0:
                holder.personBtn.setBackgroundResource(R.drawable.shape_task_progress);
               // holder.personBtn.setBackgroundColor(mContext.getResources().getColor(R.color.color_dcf3ff));
                break;
            case 1:
                holder.personBtn.setBackgroundResource(R.drawable.shape_task_progress1);
                //holder.personBtn.setBackgroundColor(mContext.getResources().getColor(R.color.color_fff2d8));
                break;
            case 2:
                holder.personBtn.setBackgroundResource(R.drawable.shape_task_progress2);
               // holder.personBtn.setBackgroundColor(mContext.getResources().getColor(R.color.color_f9fef0));
                break;
        }

        //判断是否已经编辑过 显示提示和上传按钮
        String list = taskDao.get(d.getWorkPlanId());
        List<TaskDetailEntity> infoList = json.fromJson(list, new TypeToken<List<TaskDetailEntity>>() {}.getType());
        if(list != null && list != "")
            holder.tips.setVisibility(View.VISIBLE);
        else
            holder.tips.setVisibility(View.INVISIBLE);

        //人员查看
        holder.personBtn.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Dialog dialog=new Dialog(mContext);//可以在style中设定dialog的样式
                dialog.setContentView(R.layout.item_person_window);
                WindowManager.LayoutParams lp=dialog.getWindow().getAttributes();
                lp.gravity= Gravity.CENTER;
                lp.height= WindowManager.LayoutParams.WRAP_CONTENT;
                lp.width= WindowManager.LayoutParams.MATCH_PARENT;
                dialog.getWindow().setAttributes(lp);
                //设置该属性，dialog可以铺满屏幕
                dialog.getWindow().setBackgroundDrawable(null);
                dialog.show();
                ListView list = dialog.getWindow().findViewById(R.id.listView);
                BaseAdapter adapter = new TaskPersonAdapter(Datas.get(position).getPersons(),mContext);
                list.setAdapter(adapter);
                AnimationManager.Instance().DoScaleAction(dialog.getWindow().findViewById(R.id.personWindow),0f,1f,0f,1f,200);
               // slideToUp(dialog.getWindow().findViewById(R.id.layout));
                //      dialog.getWindow().setWindowAnimations();
            }
        });

        //点击背景状态
        holder.doBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(!d.isDanger())
                    RxEventProcessor.get().post("DoTaskCallBack",d.getWorkPlanId(),false,false,false,d.getFlag() == 2 || d.getFlag() == 1 ? true : false);
                else
                    RxEventProcessor.get().post("DoTaskCallBack",d.getDangerId(),false,false,true,d.getFlag() == 2  ? true : false);
//                try {
//                    EventListenerManager.Instance().sendEvent("DoTaskCallBack",d.getWorkPlanId(),false,false);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
            }
        });
        return convertView;
    }

    public final class ViewHolder {
        public TextView posNameStr;
        public TextView contentStr;
        public TextView timeStr;
        public TextView tips;
        public Button personBtn;
        public Button summitBtn;
        public LinearLayout doBtn;
        public ImageView stateBg;
        public FocusEffct focus;
    }

}
